package com.atguigu.app.dwd.db;

import com.atguigu.util.MyKafkaUtil_lj;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author 晶晶
 * @Date 2023 03 27 18 21
 **/
public class Dwd_Trade_cartAdd {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        tableEnv.executeSql("" +
                "create table topic_db(\n" +
                "   `database` string,\n" +
                "   `table` string,\n" +
                "   `type` string,\n" +
                "   `data` map<string,string>,\n" +
                "   `old` map<string,string>,\n" +
                "   `ts` string,\n" +
                "   `proc_time` as PROCTIME()\n" +
                ")"+ MyKafkaUtil_lj.getKafkaDDL("topic_db","trade_cart_add"));


        Table cartAdd = tableEnv.sqlQuery("" +
                "select\n" +
                "   `data`['id'] id,\n" +
                "   `data`['user_id'] user_id,\n" +
                "   `data`['course_id'] course_id,\n" +
                "   `data`['course_name'] course_name,\n" +
                "   `data`['cart_price'] cart_price,\n" +
                "   `data`['session_id'] session_id,\n" +
                "   `data`['create_time'] create_time,\n" +
                "   `data`['deleted'] deleted,\n" +
                "   `data`['sold'] sold\n" +
                "from topic_db\n" +
                "where `database`='edu'\n" +
                "and `table`='cart_info'\n" +
                "and `type`='insert'");

        tableEnv.createTemporaryView("cart_info",cartAdd);

        tableEnv.executeSql("" +
                "create table dwd_trade_cart_add(\n" +
                "   `id` string,\n" +
                "   `user_id` string,\n" +
                "   `course_id` string,\n" +
                "   `course_name` string,\n" +
                "   `cart_price` string,\n" +
                "   `session_id` string,\n" +
                "   `create_time` string,\n" +
                "   `deleted` string,\n" +
                "   `sold` string\n" +
                ")"+MyKafkaUtil_lj.getKafkaSinkDDL("dwd_trade_cart_add"));

        tableEnv.executeSql("insert into  dwd_trade_cart_add select * from cart_info");

    }
}
